package com.example.shiro.utils;

import java.text.ParseException;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;

public final class UUIDUtils {
	public static String currentSNO;
	
	static{
		currentSNO = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS");
	}
	
	public static synchronized final String getTimeSno(String prefix){
		String sno = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS");
		while(currentSNO.equals(sno)){
			sno = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS");
		}
		currentSNO = sno;
		if(StringUtils.isNotEmpty(prefix)){
			sno = prefix + sno;
		}
		return sno;
	}
	
	public static final String getUUIDSno(){
		String sno = UUID.randomUUID().toString();
		sno = sno.replace("-", "");
		return sno;
	}
	
	public static final String getDateFromSno(String prfix,String sno){
		String dateStr = StringUtils.EMPTY;
		try {
			if(StringUtils.isNotEmpty(prfix)){
				dateStr = DateFormatUtils.format(DateUtils.parseDate(StringUtils.removeStart(sno, prfix),new String[]{"yyyyMMddHHmmssSSS"}),"yyyyMMdd");
			
			}else{
				dateStr = DateFormatUtils.format(DateUtils.parseDate(sno,new String[]{"yyyyMMddHHmmssSSS"}),"yyyyMMdd");
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateStr;
	}
}
